package com.zh.note.leetcode.stackqueue;

import java.util.Stack;

/**
 * 232. 用栈实现队列
 */



public class LC_232_MyQueue {

    class MyQueue {
        private Stack<Integer> stack1;
        private Stack<Integer> stack2;

        public MyQueue() {
            // 用于元素进栈
            stack1 = new Stack<>();
            // 用于元素出栈
            stack2 = new Stack<>();

        }

        public void push(int x) {
            stack1.push(x);


        }

        public int pop() {
            if (stack2.isEmpty()) {
                // 入栈不为空的时候，把入栈元素全部加入到出栈
                while (!stack1.isEmpty()) {
                    stack2.push(stack1.pop());
                }
            }
            return stack2.pop();

        }

        public int peek() {
            if (stack2.isEmpty()) {
                while (!stack1.isEmpty()) {
                    stack2.push(stack1.pop());
                }
            }
            return stack2.peek();

        }

        public boolean empty() {
            return stack1.isEmpty() && stack2.isEmpty();

        }
    }
}
